<template>
  <div class="box7">
    <div class="title">
      <p>每日消费统计</p>
      <img src="../../images/dataScreen-title.png" alt="" />
    </div>
    <div class="charts" ref="charts"></div>
  </div>
</template>

<script setup lang="ts">
import * as echarts from 'echarts'
import { ref, onMounted } from 'vue'
import { ReqDayData } from '@/api/screen'
//获取DOM节点
let charts = ref()
let arrName = ref([])
let arrValue = ref([])
let DayData = ref([])
//组件挂载完毕
onMounted(async () => {
  await getDayData()
  await init()
})
const init = () => {
  //一个容器可以同时展示多种类型的图形图标
  let mychart = echarts.init(charts.value)
  //设置配置项
  mychart.setOption({
    title: {
      text: '近七日消费统计',
      left: '40%',
      textStyle: {
        color: 'white',
      },
    },
    xAxis: {
      type: 'category',
      show: true,
      data: arrName.value,
      axisLabel: {
        color: 'white',
        fontSize: 10,
        //文字斜着显示
        // rotate: 45,
        interval: 0,
      },
    },
    yAxis: {
      type: 'value',
    },
    grid: {
      left: 30,
      bottom: 40,
    },
    series: {
      type: 'line',
      data: arrValue.value,
      //标记图形设置
      symbol: 'diamond',
      symbolSize: 20,
      //图文标签
      label: {
        show: true,
        position: 'top',
        color: 'yellowgreen',
      },
      //散点图标记的颜色
      itemStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: 'skyblue', // 0% 处的颜色
            },
            {
              offset: 0.5,
              color: 'yellowgreen', // 50% 处的颜色
            },
            {
              offset: 1,
              color: 'green', // 100% 处的颜色
            },
          ],
          global: false, // 缺省为 false
        },
      },
    },
    tooltip: {
      backgroundColor: 'rgba(8,231,205,0.9)',
      trigger: 'axis',
      formatter: function (params: any) {
        return params[0].name + ' : ' + params[0].value + ' 元'
      },
    },
  })
}
const getDayData = async () => {
  let result = await ReqDayData()
  if (result.code === 200) {
    DayData.value = result.data
    //取出rankData数组对象中name的数据放入数组中
    for (let i = 0; i < DayData.value.length; i++) {
      arrName.value.push(DayData.value[i]?.date)
      arrValue.value.push(DayData.value[i]?.num)
    }
  }
}
</script>

<style scoped lang="scss">
.box7 {
  width: 100%;
  height: 100%;
  background: url(../../images/dataScreen-main-cb.png) no-repeat;
  background-size: 100% 100%;
  margin: 20px 0px;

  .title {
    p {
      color: white;
      font-size: 18px;
    }
  }

  .charts {
    height: calc(100% - 30px);
  }
}
</style>
